﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace NUtils
{
    public class PriorityQueue<T> : IEnumerable<T>, IEnumerable
    {
        public PriorityQueue(int capacity, Comparison<T> comparison)
        {
            _comparison = comparison;
            _list = new List<T>(capacity);
        }

        public T Dequeue()
        {
            T t = _list[0];
            _list.RemoveAt(0);
            return t;
        }

        public void Insert(T t)
        {
            _list.Add(t);
            _list.Sort(_comparison);
        }

        public void PriorityChanged()
        {
            _list.Sort(_comparison);
        }

        public bool Contains(T t)
        {
            return _list.Contains(t);
        }

        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        public bool Empty
        {
            get
            {
                return _list.Count == 0;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        private Comparison<T> _comparison;
        private List<T> _list;
    }
}
